﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.Commands;

namespace Mimoza.Apps.Console.ViewModel
{
    public class UpdateViewModel : BaseViewModel
    {
        private Container m_currentContainer = null;
        public Container CurrentContainer
        {
            get { return m_currentContainer; }
            set
            {
                if (m_currentContainer != value)
                {
                    m_currentContainer = value;
                    NotifyPropertyChanged("CurrentContainer");
                }
            }
        }

        public override void Init(Mimoza.Apps.Console.Application application)
        {
            base.Init(application);
            
            this.InFocus = false;

            m_application.ContainersTreeViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ContainersTreeViewModel_PropertyChanged);
            m_application.ObjectLayer.UpdateRepository.OnObjectChanged += new EventHandler<Mimoza.ObjectLayer.ObjectLayer_ObjectChangedEventArgs>(UpdateRepository_OnObjectChanged);

            m_ToolBarCommands.Add(new NewUpdateCommand(this));
            m_ToolBarCommands.Add(new DeleteUpdateCommand(this));

            m_ContextCommands.Add(new DeleteUpdateCommand(this));
        }

        void UpdateRepository_OnObjectChanged(object sender, Mimoza.ObjectLayer.ObjectLayer_ObjectChangedEventArgs e)
        {
            RaiseOnObjectChanged(e);
        }

        void ContainersTreeViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Context")
            {
                Container cont = (sender as BaseViewModel).Context as Container;

                if (null != cont && cont.Type != ContainerType.Updates )
                {
                    //this.CurrentContainer = null;
                    this.Visible = false;
                }
                else 
                {
                    //this.CurrentContainer = cont;
                    this.Visible = true;
                }                
            }
        }
    }
}
